unit CronUtils;

interface

uses
  Classes, SysUtils, INIfiles, Contnrs;

Type
  TCronItem = Class
    hour: integer;
    Description:String;
  End;

  TCronUtils = class
    private
      FCronList: TObjectList;
      function GetCronMask: String;
    function GetCronItem(index: integer): TCronItem;
    function GetCount: integer;
    public
      Constructor Create;
      Destructor Destroy; override;
      procedure Add(hour: integer; Description:String);
      procedure Clear;
      procedure Delete(hour:integer);
      procedure SaveToFile(Filename:String);
      procedure LoadFromFile(FileName:String);

      property CronMask:String read GetCronMask;
      property Items[index:integer] :TCronItem Read GetCronItem;
      property Count: integer read GetCount;
  end;


implementation

{ TCronUtils }

procedure TCronUtils.Add(hour: integer; Description: String);
var
  I: Integer;
  Item: TCronItem;
  Flag: Boolean;
begin
  Item := TCronItem.Create;
  Item.hour := Hour;
  Item.Description := Description;

  Flag := False;
  for I := 0 to FCronList.Count - 1 do
    if TCronItem( FCronList.Items[i] ).hour  = hour then begin
      Flag := True;
      break;
    end;

  if not Flag then FCronList.Add( Item );
end;

procedure TCronUtils.Clear;
begin
  FCronList.Clear;
end;

constructor TCronUtils.Create;
begin
  FCronList := TObjectList.Create(True);
end;

procedure TCronUtils.Delete(hour: integer);
var
  I, index: Integer;
begin
  index := -1;
  for I := 0 to FCronList.Count - 1 do
    if TCronItem( FCronList.Items[i] ).hour = Hour then begin
      index := i;
      break;
    end;

  if index <> -1 then
    FCronList.Delete(index);
end;

destructor TCronUtils.Destroy;
begin
  FCronList.Free;
  inherited;
end;

function TCronUtils.GetCount: integer;
begin
  Result := FCronList.Count;
end;

function TCronUtils.GetCronItem(index: integer): TCronItem;
begin
  Result := TCronItem( FCronList.Items[index] );
end;

function TCronUtils.GetCronMask: String;
var
  mask:String;
  i: integer;
  SortList: TStringList;
begin
  SortList := TStringList.Create;
  SortList.Sorted := True;
  try
    for i := 0 to FCronList.Count - 1 do
      SortList.Add( Format('%.2d', [ TCronItem( FCronList.Items[i] ).hour ]) );
    mask := '';
    for i := 0 to SortList.Count - 1 do begin
      if mask <> '' then Mask := mask  + ',';
      mask := mask + intToStr( StrToInt( SortList.Strings[i] ) );
    end;
  finally
    Sortlist.Free;
  end;
  if Mask <> '' then
   Result := '0 0 ' + Mask
  else Result := '';
end;

procedure TCronUtils.LoadFromFile(FileName: String);
var
  I: Integer;
  INI: TInifile;
  Sections:TStringLIst;
  hour: integer;
  description:String;
begin
  INI := TIniFile.Create(FileName);
  Sections := TStringList.Create;
  try
    ini.ReadSections( Sections );
    for I := 0 to Sections.Count - 1 do Begin
      hour := ini.ReadInteger(Sections.Strings[i], 'hour', -1);
      description := ini.ReadString(Sections.Strings[i], 'Description', '');
      if (hour >=0) and (hour <=24) then  self.Add( Hour, description );
    End;
  finally
    Sections.Free;
    INI.Free;
  end;
end;

procedure TCronUtils.SaveToFile(Filename: String);
var
  I: Integer;
  INI: TInifile;
  Item: TCronItem;
begin
  if FileExists(FileName) then DeleteFile( FileName );
  INI := TIniFile.Create(fileName);
  try
    for I := 0 to FCronList.Count - 1 do Begin
      Item := TCronItem ( FCronList.Items[i] );
      INI.WriteInteger( intToStr(Item.hour), 'Hour', Item.hour );
      Ini.WriteString (  intToStr(Item.hour), 'Description', Item.Description);
    End;
  finally
    INI.Free;
  end;
end;

end.
